/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ProductGUIs;

import Model.InternalDBManager;
import Model.Product;
import Model.Store;
import Supermarket.AppMessages;
import java.util.ListIterator;
import javax.persistence.EntityManager;
import javax.swing.ImageIcon;

/**
 *
 * @author Manolis
 */
public class ProductsStoresFrame extends javax.swing.JFrame {

    private static EntityManager em;
    private Store selectedStore;

    /**
     * Creates new form ProductsStoresFrame
     */
    public ProductsStoresFrame() {
        em = InternalDBManager.getEm();
        if (!em.getTransaction().isActive()){
                em.getTransaction().begin();
        }

        this.setIconImage(new
                ImageIcon(getClass().getResource("..\\Images\\cart.png")).getImage());
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        productQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT p FROM Product p");
        productList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(productQuery.getResultList());
        productQuery1 = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT p FROM Product p");
        productList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(productQuery1.getResultList());
        storeQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT s FROM Store s");
        storeList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(storeQuery.getResultList());
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        StoresComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        AddProductToStoreButton = new javax.swing.JButton();
        RemoveProductFromStoreButton = new javax.swing.JButton();
        AddAllProductsToStoreButton = new javax.swing.JButton();
        RemoveAllProductsFromStoreButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        StoreProductsList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        AvailableProductsList = new javax.swing.JList();
        MessageLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Συσχέτιση Προϊόντων - Καταστημάτων");
        setAlwaysOnTop(true);
        setBackground(java.awt.Color.white);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Κατάστημα:");

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${resultList}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, storeQuery, eLProperty, StoresComboBox);
        bindingGroup.addBinding(jComboBoxBinding);

        StoresComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StoresComboBoxActionPerformed(evt);
            }
        });

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Panel truck_50.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(137, Short.MAX_VALUE)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(StoresComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(StoresComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Διαθέσιμα Προϊόντα");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Προϊόντα Καταστήματος");

        AddProductToStoreButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Extras Forward_50.png"))); // NOI18N
        AddProductToStoreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddProductToStoreButtonActionPerformed(evt);
            }
        });

        RemoveProductFromStoreButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Extras Backward_50.png"))); // NOI18N
        RemoveProductFromStoreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveProductFromStoreButtonActionPerformed(evt);
            }
        });

        AddAllProductsToStoreButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/FastForward_48.png"))); // NOI18N
        AddAllProductsToStoreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddAllProductsToStoreButtonActionPerformed(evt);
            }
        });

        RemoveAllProductsFromStoreButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/FastBackward_48.png"))); // NOI18N
        RemoveAllProductsFromStoreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveAllProductsFromStoreButtonActionPerformed(evt);
            }
        });

        StoreProductsList.setBackground(new java.awt.Color(255, 255, 204));
        StoreProductsList.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        StoreProductsList.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, productList1, StoreProductsList);
        jListBinding.setDetailBinding(org.jdesktop.beansbinding.ELProperty.create("${productName}"));
        bindingGroup.addBinding(jListBinding);

        jScrollPane1.setViewportView(StoreProductsList);

        AvailableProductsList.setBackground(new java.awt.Color(255, 255, 204));
        AvailableProductsList.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        AvailableProductsList.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, productList, AvailableProductsList);
        jListBinding.setDetailBinding(org.jdesktop.beansbinding.ELProperty.create("${productName}"));
        bindingGroup.addBinding(jListBinding);

        jScrollPane2.setViewportView(AvailableProductsList);

        MessageLabel.setForeground(java.awt.Color.red);
        MessageLabel.setText(" ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(56, 56, 56)
                                .addComponent(jLabel2))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(RemoveProductFromStoreButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                        .addComponent(RemoveAllProductsFromStoreButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(AddProductToStoreButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(AddAllProductsToStoreButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(MessageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 493, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(AddProductToStoreButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AddAllProductsToStoreButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(RemoveProductFromStoreButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RemoveAllProductsFromStoreButton))
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1))
                .addGap(12, 12, 12)
                .addComponent(MessageLabel)
                .addContainerGap())
        );

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setIcon(new javax.swing.ImageIcon("C:\\Users\\Manolis\\Documents\\NetBeansProjects\\plh24_3h_Ergasia\\src\\Images\\supermarket_320.png")); // NOI18N
        jLabel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    //προσθέτει τα επιλεγμένα προϊόντα στο κατάστημα
    private void AddProductToStoreButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddProductToStoreButtonActionPerformed
        if (!em.getTransaction().isActive()){
            em.getTransaction().begin();
        }
        //ένας πίνακας πάνω στον οποίο αποθηκεύονται οι δείκτες των επιλεγμέων προϊόντων
        int[] selectedFromAvailable = AvailableProductsList.getSelectedIndices();
        if (StoresComboBox.getSelectedIndex() >=0){
            selectedStore = storeList.get(StoresComboBox.getSelectedIndex());
            MessageLabel.setText(" ");
        }else{
            MessageLabel.setText("Δεν υπάρχει επιλεγμένο κατάστημα");
            return;
        }
        for (int i=0; i<selectedFromAvailable.length;i++){
            //διάβασε το επιλεγμένο διαθέσιμο προϊόν (βάσει του δείκτη του)
            if (AvailableProductsList.getSelectedIndex()>-1){
                //επιλέγεται κάθε φορά το επιλεγμένο προϊόν με τον μικρότερο index
                Product product = productList.get(AvailableProductsList.getSelectedIndex());
                //το αφαιρεί από τα διαθέσιμα
                productList.remove(product);
                //αν δεν υπάρχει στα συσχετισμένα προϊόντα του καταστήματος
                if (!selectedStore.getProductList().contains(product)){
                    //κάνει συσχετίσεις καταστήματος - προϊόντος
                    selectedStore.addToProductList(product);
                    product.addToStoreList(selectedStore);
                    //το προσθέτει στη λίστα προϊόντων του καταστήματος
                    productList1.add(product);
                    em.persist(product);
                }
            }
        }
        em.persist(selectedStore);
        em.getTransaction().commit();
    }//GEN-LAST:event_AddProductToStoreButtonActionPerformed


    //προσθέτει όλα τα διαθέσιμα προϊόντα στο κατάστημα
    private void AddAllProductsToStoreButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddAllProductsToStoreButtonActionPerformed
         if (!em.getTransaction().isActive()){
            em.getTransaction().begin();
        }
        //αν δεν είναι έγκυρο το κατάστημα επέστρεψε
        if (!ValidStore()) return;
        //ένας ListIterator πάνω στα περιεχόμενα της λίστας διαθέσιμων προϊόντων
        for(ListIterator<Product> iterator = productList.listIterator(); iterator.hasNext();){
            //παίρνουμε ένα-ενα όλα τα προϊόντα 
            Product product = iterator.next();
            //αν υπάρχουν συσχετίσεις προϊόντων - καταστήματος
            if (!selectedStore.getProductList().contains(product)){
                //τις αφαιρούμε
                selectedStore.addToProductList(product);
                product.addToStoreList(selectedStore);
                //ενημερώνουμε κάθε το προϊόν
                em.persist(product);
            }
            //αν δεν υπάρχει το προϊόν τότε το προσθέτουμε στη λίστα του καταστήματος
            if (!productList1.contains(product)){
                productList1.add(product);
            }
            //διέγραψε ότι έχει επιλεγεί με το iterator.next()
            iterator.remove();
        }
        //ενημερώνουμε συνολικά το κατάστημα
        em.persist(selectedStore);
        em.getTransaction().commit();
    }//GEN-LAST:event_AddAllProductsToStoreButtonActionPerformed


    //αφαιρεί τα επιλεγμένα προϊόντα από τη λίστα του καταστήματος
    private void RemoveProductFromStoreButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveProductFromStoreButtonActionPerformed
         if (!em.getTransaction().isActive()){
            em.getTransaction().begin();
        }
        //αν δεν είναι έγκυρο το κατάστημα επέστρεψε
        if (!ValidStore()) return;
        //ένας πίνακας πάνω στον οποίο αποθηκεύονται οι δείκτες των επιλεγμέων προϊόντων
        int[] selectedFromStore = StoreProductsList.getSelectedIndices();
        //για όλα τα επιλεγμένα
        for (int i=0; i<selectedFromStore.length; i++){
            //διάβασε το επιλεγμένο διαθέσιμο προϊόν (βάσει του δείκτη του)
            if (StoreProductsList.getSelectedIndex()>-1){
                //επιλέγεται κάθε φορά το προϊόν με τον μικρότερο index
                Product product = productList1.get(StoreProductsList.getSelectedIndex());
                //το αφαιρεί από τα διαθέσιμα
                productList1.remove(product);
                //αν υπάρχει στα συσχετισμένα προϊόντα του καταστήματος
                if (selectedStore.getProductList().contains(product)){
                    //διαγράφει συσχετίσεις καταστήματος - προϊόντος
                    selectedStore.removeFromProductList(product);
                    product.removeFromStoreList(selectedStore);
                    em.persist(product);
                }
                if (!productList.contains(product)){
                    //το προσθέτει στη λίστα προϊόντων του καταστήματος
                    productList.add(product);
                }
            }
        }
        em.persist(selectedStore);
        em.getTransaction().commit();
    }//GEN-LAST:event_RemoveProductFromStoreButtonActionPerformed


    //αφαιρεί όλα τα προϊόντα από τη λίστα του καταστήματος
    private void RemoveAllProductsFromStoreButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveAllProductsFromStoreButtonActionPerformed
         if (!em.getTransaction().isActive()){
            em.getTransaction().begin();
        }
        //αν δεν είναι έγκυρο το κατάστημα επέστρεψε
        if (!ValidStore()) return;
        //ένας ListIterator πάνω στα περιεχόμενα της λίστας προιόντων καταστήματος
        for(ListIterator<Product> iterator = productList1.listIterator(); iterator.hasNext();){
            //παίρνουμε ένα-ενα όλα τα προϊόντα 
            Product product = iterator.next();
            //αν υπάρχουν συσχετίσεις προϊόντων - καταστήματος
            if (selectedStore.getProductList().contains(product)){
                //τις αφαιρούμε
                selectedStore.removeFromProductList(product);
                product.removeFromStoreList(selectedStore);
                //ενημερώνουμε κάθε το προϊόν
                em.persist(product);
            }
            //αν δεν υπάρχει το προϊόν τότε το προσθέτουμε στη λίστα διαθέσιμων
            if (!productList.contains(product)){
                productList.add(product);
            }
            iterator.remove();
        }
        //ενημερώνουμε συνολικά το κατάστημα
        em.persist(selectedStore);
        em.getTransaction().commit();
    }//GEN-LAST:event_RemoveAllProductsFromStoreButtonActionPerformed

    //διαβάζει την επιλογή καταστήματος και αρχικοποιεί ανάλογα τις λίστες
    private void StoresComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StoresComboBoxActionPerformed
        if (StoresComboBox.getSelectedIndex() > -1 ){
            selectedStore = storeList.get(StoresComboBox.getSelectedIndex());
            populateLists(selectedStore);
        }
    }//GEN-LAST:event_StoresComboBoxActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProductsStoresFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ProductsStoresFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddAllProductsToStoreButton;
    private javax.swing.JButton AddProductToStoreButton;
    private javax.swing.JList AvailableProductsList;
    private javax.swing.JLabel MessageLabel;
    private javax.swing.JButton RemoveAllProductsFromStoreButton;
    private javax.swing.JButton RemoveProductFromStoreButton;
    private javax.swing.JList StoreProductsList;
    private javax.swing.JComboBox StoresComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private java.util.List<Model.Product> productList;
    private java.util.List<Model.Product> productList1;
    private javax.persistence.Query productQuery;
    private javax.persistence.Query productQuery1;
    private java.util.List<Model.Store> storeList;
    private javax.persistence.Query storeQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables


    //αρχικοποιεί τις λίστες
    private void populateLists(Store selectedStore) {
        if (!em.getTransaction().isActive()){
                em.getTransaction().begin();
        }
        try{
            productList.clear();
            productList.addAll(em.createNamedQuery("Product.findAll").getResultList());
            productList1.clear();
            productList1.addAll(selectedStore.getProductList());
            productList.removeAll(productList1);
        }catch(Exception e){
            AppMessages.FailureMessage(this, e);
        }
    }

    private boolean ValidStore() {
         //αν υπάρχει έγκυρη επιλογή καταστήματος
        if (StoresComboBox.getSelectedIndex() >=0){
            selectedStore = storeList.get(StoresComboBox.getSelectedIndex());
            MessageLabel.setText(" ");
            return true;
        }else{
            MessageLabel.setText("Δεν υπάρχει επιλεγμένο κατάστημα");
            return false;
        }
    }


}
